// @flow
import React, { Component } from 'react'
import styles from './VarSettings.css'
import { Button, message, Popconfirm } from 'antd'
// import VarModalContent from 'components/VarComp/VarModalContent'
import VarEdit from 'components/VarEditDevice'
import VarLimit from 'components/VarLimitDevice'
import universalFetch, { handleFetchError } from 'store/modules/fetch'
import ReactDom from 'react-dom'

type Props = {
  data: Object,
  refreshVar: Function,
  refreshLimit: Function
}
type States = {
  visibleVariable: boolean,
  visiblePoint: boolean,
  confirmLoading: boolean,
  variableType: Array<Object>
}
class VarSettings extends Component {
  props: Props
  state: States
  editVar: Function
  removeEditVar: Function
  deleteVar: Function
  addLimits: Function
  removeVarLimit: Function
  constructor (props: Props) {
    super(props)
    this.state = {
      visibleVariable: false,
      confirmLoading: false,
      visiblePoint: false,
      variableType: []
    }
    this.editVar = this.editVar.bind(this)
    this.removeEditVar = this.removeEditVar.bind(this)
    this.deleteVar = this.deleteVar.bind(this)
    this.addLimits = this.addLimits.bind(this)
    this.removeVarLimit = this.removeVarLimit.bind(this)
  }

  deleteVar () {
    const { data } = this.props
    universalFetch(`${__SERVICE_API__}device/monitors/variables/${data.id}`, {
      method: 'DELETE',
      headers: {
        'Content-Type': 'application/json'
      }
    })
      .then(res => res.json())
      .then((json) => {
        if (json.code !== 200000) {
          throw new Error(JSON.stringify({ code: json.code, message: json.message }))
        }
        message.success('变量删除成功')
        this.props.refreshVar()
      })
      .catch((error) => {
        handleFetchError(error)
        console.error(error)
      })
  }

  removeEditVar () {
    const container = this.refs.editVar
    ReactDom.unmountComponentAtNode(container)
  }
  editVar () {
    const container = this.refs.editVar
    const { refreshVar, data } = this.props
    ReactDom.render(
      <VarEdit {...{
        refreshVar,
        data,
        removeNode: this.removeEditVar
      }} />,
    container
    )
  }

  removeVarLimit () {
    const container = this.refs.addLimits
    ReactDom.unmountComponentAtNode(container)
  }
  addLimits () {
    const container = this.refs.addLimits
    const { data, refreshLimit } = this.props
    ReactDom.render(
      <VarLimit {...{
        data,
        refreshLimit,
        removeNode: this.removeVarLimit
      }} />,
    container
    )
  }
  render () {
    return (
      <div className={styles['actions']}>
        <Button size='small'
          type='primary'
          style={{ marginRight: '10px' }}
          onClick={this.editVar}
          >
          编辑
        </Button>
        <Popconfirm
          title='确定要删除此变量吗？'
          onConfirm={this.deleteVar}
          >
          <Button size='small'
            type='danger'
            style={{ marginRight: '10px' }}
            >
            删除
          </Button>
        </Popconfirm>
        <Button
          size='small'
          onClick={this.addLimits}
          >
          添加阈值
        </Button>
        <div ref='editVar' />
        <div ref='addLimits' />
      </div>
    )
  }
}

export default VarSettings
